<template>
  <div class="dashboard-container">
    <div class="app-container">
 <el-card>

   <el-tabs v-model="activeName">
<!--     角色管理-->
     <el-tab-pane label="角色管理" name="first" >

      <el-row style="height: 60px">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="addrole" >添加角色</el-button>
      </el-row>

       <el-table style="width: 100%"
                 :data="list"
                 :stripe="true" :border="true" :highlight-current-row="true">
            <el-table-column label="序号" width="120" type="index" align="center"></el-table-column>
            <el-table-column label="角色名称" width="240" prop="name" align="center"></el-table-column>
            <el-table-column label="描述" prop="description" align="center"></el-table-column>
         <el-table-column label="操作" align="center">
      <template slot-scope="{ row }">
        <el-button size="small" type="success">分配权限</el-button>
        <el-button size="small" type="primary" @click="editrole(row)">编辑</el-button>
        <el-button size="small" type="danger" @click="deleterole(row)">删除</el-button>
      </template>
         </el-table-column>
       </el-table>

       <el-row type="flex" justify="center" align="middle" style="height: 60px">
         <!-- 分页组件 -->
         <el-pagination :current-page="pages.page"
                        :page-size="pages.pagesize"
                        :total="pages.total"
                        layout="total,prev,pager,next"
                         @current-change="changepage"></el-pagination>
       </el-row>

     </el-tab-pane>

<!--公司管理-->
     <el-tab-pane label="公司管理" name="second">


       <el-alert
         title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
         type="info"
         show-icon
         :closable="false"
       />

       <el-form label-width="120px" style="margin-top:50px">
         <el-form-item label="公司名称">
           <el-input v-model="formaData.name" disabled style="width:400px"></el-input>
         </el-form-item>

         <el-form-item label="公司地址">
           <el-input v-model="formaData.companyAddress" disabled style="width:400px"></el-input>
         </el-form-item>

         <el-form-item label="邮箱">
           <el-input disabled  v-model="formaData.mailbox"  style="width:400px"></el-input>
         </el-form-item>

         <el-form-item label="手机">
           <el-input disabled  v-model="formaData.companyPhone"  style="width:400px"></el-input>
         </el-form-item>

         <el-form-item label="备注">
           <el-input   v-model="formaData.remarks" type="textarea" :rows="3" disabled style="width:400px" ></el-input>
         </el-form-item>

       </el-form>

     </el-tab-pane>

   </el-tabs>
<!-- 弹窗-->
   <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
     <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
       <el-form-item label="角色名称" prop="name">
         <el-input v-model="roleForm.name" />
       </el-form-item>
       <el-form-item label="角色描述">
         <el-input v-model="roleForm.description" />
       </el-form-item>
     </el-form>
     <!-- 底部 -->
     <el-row slot="footer" type="flex" justify="center">
       <el-col :span="6">
         <el-button size="small" @click="btnCancel">取消</el-button>
         <el-button size="small" type="primary" @click="btnOK">确定</el-button>
       </el-col>
     </el-row>
   </el-dialog>
 </el-card>
    </div>
  </div>
</template>

<script>
import {getRoleList,getCompanyInfo,deleteRole,updateRole,getRoleDetail,addRole} from "@/api/setting";
import {mapGetters} from 'vuex'

export default {

  data() {
    return {
      //默认显示第几个选项卡
      activeName: 'first',
      //弹窗状态
      showDialog:false,
      //角色表格的数据
      list:[],

      pages: {
        // 放置页码及相关数据
        page: 1,
        //每页显示多少条数
        pagesize:10,
        total: 0 // 记录总数

      },
      //公司信息
      formaData:{},
      //角色详细信息
      roleForm:{},
      //表单验证规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },


    };
  },
  computed:{
    ...mapGetters(['companyId']),
    showTitle(){
      return this.roleForm.id ? '编辑角色' : '添加角色'
    }


  },
  created() {
      this.getRolelist()
    this.getcompanyInfo()
    },
  methods: {


//获取角色列表
   async getRolelist(){
      const {total,rows} = await  getRoleList(this.pages)
    this.pages.total=total
     this.list=rows

    },
    //改变页码时触发的函数
    changepage(newpage){
      this.pages.page=newpage
      this.getRolelist()

    },
    //获取公司信息
    async getcompanyInfo(){
      this.formaData=await getCompanyInfo(this.companyId)
    },
  //删除角色
  async  deleterole(row){
    console.log(row)
      try{
        await  this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async ()=>{
         await deleteRole(row.id)
          this.getRolelist()
          this.$message.success('删除角色成功')
        })
      }catch (error){
        console.log(error)
      }

    },

    //弹窗关闭
    btnCancel(){
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog=false
    },
  //  编辑角色
    async  editrole(row){
      this.roleForm=await getRoleDetail(row.id)
      this.showDialog=true
      console.log(this.roleForm)

    },

    //添加角色弹窗
    addrole(){
     this.showDialog=true
    },

  //  点击确认提交数据
   async btnOK(){

     try {
       await this.$refs.roleForm.validate()
       if (this.roleForm.id){
       //  如果有id则为编辑，没有就是新增
         await updateRole(this.roleForm)

       }else {

       //  添加角色信息
      await addRole(this.roleForm)
       }
       this.getRolelist()
       this.$message.success('更新数据成功')
       this.showDialog=false




     }catch (error){
       console.log(error)



     }

    }

  },


}
</script>

<style>

</style>
